package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.app.bo.AppMerchantOrderBo;
import com.ruoyi.business.app.bo.AppOrderEvaluateBo;
import com.ruoyi.business.app.vo.AppOrderEvaluatePicsVo;
import com.ruoyi.business.app.vo.AppOrderEvaluateVo;
import com.ruoyi.business.bo.HcxOrderEvaluateAddBo;
import com.ruoyi.business.bo.HcxOrderEvaluateEditBo;
import com.ruoyi.business.bo.HcxOrderEvaluateQueryBo;
import com.ruoyi.business.domain.HcxAppUser;
import com.ruoyi.business.domain.HcxMerchantOrder;
import com.ruoyi.business.domain.HcxOrderEvaluate;
import com.ruoyi.business.domain.HcxOrderEvaluatePics;
import com.ruoyi.business.mapper.HcxOrderEvaluateMapper;
import com.ruoyi.business.service.IHcxAppUserService;
import com.ruoyi.business.service.IHcxMerchantOrderService;
import com.ruoyi.business.service.IHcxOrderEvaluatePicsService;
import com.ruoyi.business.service.IHcxOrderEvaluateService;
import com.ruoyi.business.vo.HcxOrderEvaluateVo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单评价Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Service
@AllArgsConstructor
public class HcxOrderEvaluateServiceImpl extends ServicePlusImpl<HcxOrderEvaluateMapper, HcxOrderEvaluate> implements IHcxOrderEvaluateService {


    private final IHcxOrderEvaluatePicsService orderEvaluatePicsService;

    private final IHcxAppUserService appUserService;

    private final IHcxMerchantOrderService merchantOrderService;


    @Override

    public HcxOrderEvaluateVo queryById(Long id) {
        return getVoById(id, HcxOrderEvaluateVo.class);
    }

    @Override
    public TableDataInfo<HcxOrderEvaluateVo> queryPageList(HcxOrderEvaluateQueryBo bo) {
        PagePlus<HcxOrderEvaluate, HcxOrderEvaluateVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxOrderEvaluateVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxOrderEvaluateVo> queryList(HcxOrderEvaluateQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxOrderEvaluateVo.class);
    }

    private LambdaQueryWrapper<HcxOrderEvaluate> buildQueryWrapper(HcxOrderEvaluateQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxOrderEvaluate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HcxOrderEvaluate::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getPhonenumber()), HcxOrderEvaluate::getPhonenumber, bo.getPhonenumber());
        lqw.like(StrUtil.isNotBlank(bo.getRealName()), HcxOrderEvaluate::getRealName, bo.getRealName());
        lqw.eq(bo.getOrderId() != null, HcxOrderEvaluate::getOrderId, bo.getOrderId());
        lqw.eq(StrUtil.isNotBlank(bo.getOrderNum()), HcxOrderEvaluate::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getShouldPrice() != null, HcxOrderEvaluate::getShouldPrice, bo.getShouldPrice());
        lqw.eq(bo.getActualPrice() != null, HcxOrderEvaluate::getActualPrice, bo.getActualPrice());
        lqw.eq(bo.getOrderTime() != null, HcxOrderEvaluate::getOrderTime, bo.getOrderTime());
        lqw.eq(bo.getStarsNum() != null, HcxOrderEvaluate::getStarsNum, bo.getStarsNum());
        lqw.eq(StrUtil.isNotBlank(bo.getEvalueContent()), HcxOrderEvaluate::getEvalueContent, bo.getEvalueContent());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxOrderEvaluate::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxOrderEvaluateAddBo bo) {
        HcxOrderEvaluate add = BeanUtil.toBean(bo, HcxOrderEvaluate.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxOrderEvaluateEditBo bo) {
        HcxOrderEvaluate update = BeanUtil.toBean(bo, HcxOrderEvaluate.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxOrderEvaluate entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxOrderEvaluate(List<HcxOrderEvaluateVo> hcxOrderEvaluateList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxOrderEvaluateList), "导入数据不能为空");
        int successNum = 0;
        for (HcxOrderEvaluateVo hcxOrderEvaluate : hcxOrderEvaluateList) {
            HcxOrderEvaluate add = BeanUtil.toBean(hcxOrderEvaluate, HcxOrderEvaluate.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxOrderEvaluateList.size(), successNum);
        if (successNum != hcxOrderEvaluateList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxOrderEvaluateList.size() - successNum);
        }
        return msg;
    }

    @Override
    public TableDataInfo<AppOrderEvaluateVo> getOrderEvaluateList(AppMerchantOrderBo bo) {
        List<HcxMerchantOrder> list = merchantOrderService.list(new LambdaQueryWrapper<HcxMerchantOrder>().eq(HcxMerchantOrder::getMerchantId, bo.getMerchantId()));
        PagePlus<HcxOrderEvaluate, AppOrderEvaluateVo> result;
        if (ObjectUtil.isNotEmpty(list)) {
            List<Long> collect = list.stream().map(HcxMerchantOrder::getId).collect(Collectors.toList());
            result = pageVo(PageUtils.buildPagePlus(), new LambdaQueryWrapper<HcxOrderEvaluate>()
                    .in(ObjectUtil.isNotEmpty(list),
                            HcxOrderEvaluate::getOrderId,
                            collect), AppOrderEvaluateVo.class);
        } else {
            result = pageVo(PageUtils.buildPagePlus(), Wrappers.emptyWrapper(), AppOrderEvaluateVo.class);
        }

        // 查询订单评价的图片
        List<AppOrderEvaluateVo> recordsVo = result.getRecordsVo();
        for (AppOrderEvaluateVo appOrderEvaluateVo : recordsVo) {
            List<AppOrderEvaluatePicsVo> photosList = orderEvaluatePicsService.listVo(new LambdaQueryWrapper<HcxOrderEvaluatePics>().eq(HcxOrderEvaluatePics::getOrderEvaluateId,
                    appOrderEvaluateVo.getId()), AppOrderEvaluatePicsVo.class);
            appOrderEvaluateVo.setPhotos(photosList);
        }
        return PageUtils.buildDataInfo(result);
    }

    @Override
    @Transactional
    public boolean evaluate(AppOrderEvaluateBo bo) {
        HcxAppUser hcxAppUser = appUserService.getById(SecurityUtils.getUserId());
        HcxOrderEvaluate hcxOrderEvaluate = new HcxOrderEvaluate();
        hcxOrderEvaluate.setUserId(SecurityUtils.getUserId());
        hcxOrderEvaluate.setPhonenumber(hcxAppUser.getPhonenumber());
        hcxOrderEvaluate.setRealName(hcxAppUser.getRealName());
        hcxOrderEvaluate.setAvatar(hcxAppUser.getAvatar());
        HcxMerchantOrder hcxMerchantOrder = merchantOrderService.getById(bo.getOrderId());
        hcxOrderEvaluate.setOrderId(hcxMerchantOrder.getId());
        hcxOrderEvaluate.setOrderNum(hcxMerchantOrder.getOrderNum());
        hcxOrderEvaluate.setShouldPrice(hcxMerchantOrder.getShouldPrice());
        hcxOrderEvaluate.setActualPrice(hcxMerchantOrder.getActualPrice());
        hcxOrderEvaluate.setOrderTime(hcxMerchantOrder.getCreateTime());
        hcxOrderEvaluate.setStarsNum(bo.getStarsNum());
        hcxOrderEvaluate.setEvalueContent(bo.getEvalueContent());
        boolean save = save(hcxOrderEvaluate);

        // 保存评价照片
        List<String> list = bo.getPicList();

        List<HcxOrderEvaluatePics> picList = new ArrayList<>();
        for (String pic : list) {
            HcxOrderEvaluatePics hcxOrderEvaluatePics = new HcxOrderEvaluatePics();
            hcxOrderEvaluatePics.setOrderId(bo.getOrderId());
            hcxOrderEvaluatePics.setOrderEvaluateId(hcxOrderEvaluate.getId());
            hcxOrderEvaluatePics.setPicUrl(pic);
            picList.add(hcxOrderEvaluatePics);
        }
        save = orderEvaluatePicsService.saveAll(picList);

        // 修改订单状态 为已完成
        hcxMerchantOrder.setStatus(Constants.THREE);

        save = merchantOrderService.updateById(hcxMerchantOrder);
        return save;
    }
}
